package cn.ffcs.uec.staff.action;

import java.text.ParseException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.common.vo.UecWaPrmlv;
import cn.ffcs.uec.common.vo.UecWaSeclv;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.salary.manager.UecSalaryProjectManager;
import cn.ffcs.uec.salary.model.UecSalaryProject;
import cn.ffcs.uec.staff.action.bean.UecPsndocWadocEditBean;
import cn.ffcs.uec.staff.model.UecPsndocWadoc;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecPsndocWadocEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecPsndocWadocEditBean bean = new UecPsndocWadocEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 薪资标准等级 == 岗位等级
	 */
	private UecWaPrmlv uecWaPrmlv;
	/**
	 * 薪资标准档别
	 */
	private UecWaSeclv uecWaSeclv;
	/**
	 * 薪资项目
	 */
	private UecSalaryProject uecSalaryProject;

	/**
	 * 薪资变动情况
	 */
	private UecPsndocWadoc uecPsndocWadoc;

	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");

	private UecSalaryProjectManager uecSalaryProjectManager = (UecSalaryProjectManager) ApplicationContextUtil
			.getBean("uecSalaryProjectManager");

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecPsndocWadocEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 薪资变动原因
		ListboxUtils.rendererForEdit(bean.getPkChangecause(),
				NodeVoUtil.getDictionaries(UecConstants.WAGE_CHANGE_CAUSE));
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("view".equals(opType)) {
			this.bean.getUecPsndocWadocEditWindow().setTitle("查看薪资变动情况信息");
			uecPsndocWadoc = (UecPsndocWadoc) arg.get("uecPsndocWadoc");
			if (null != uecPsndocWadoc) {
				PubUtil.fillBeanFromPo(uecPsndocWadoc, bean);
				// 是否谈判工资
				if (!StrUtil.isEmpty(uecPsndocWadoc.getNegotiationWage())) {
					if ("Y".equals(uecPsndocWadoc.getNegotiationWage())) {
						bean.getNegotiationWage().setValue("是");
					} else if ("N".equals(uecPsndocWadoc.getNegotiationWage())) {
						bean.getNegotiationWage().setValue("否");
					}
				}
				// 是否来自于流程
				if (!StrUtil.isEmpty(uecPsndocWadoc.getWorkflowflag())) {
					if ("Y".equals(uecPsndocWadoc.getWorkflowflag())) {
						bean.getWorkflowflag().setValue("是");
					} else if ("N".equals(uecPsndocWadoc.getWorkflowflag())) {
						bean.getWorkflowflag().setValue("否");
					}
				}
				// 薪资标准类别
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaGrd())) {
					if ("1001A1100000000B2DPQ".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("集团薪资标准（新岗新体系）");
					} else if ("1001A1100000000A7FIO".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("集团薪资标准（旧岗新体系）");
					} else if ("1001A1100000000A83WH".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("内蒙古薪资标准（旧岗新体系）");
					} else if ("1001A1100000000TRON2".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("薪资表（旧体系）");
					}
				}
				// 薪资标准
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaSeclv())
						&& !StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaPrmlv())) {
					// 薪资级别==岗位等级
					uecWaPrmlv = new UecWaPrmlv();
					uecWaPrmlv.setPkWaPrmlv(uecPsndocWadoc.getPkWaPrmlv());
					uecWaPrmlv = customFilesManager.findObjectByUecWaPrmlv(uecWaPrmlv);
					// 薪资档别
					uecWaSeclv = new UecWaSeclv();
					uecWaSeclv.setPkWaSeclv(uecPsndocWadoc.getPkWaSeclv());
					uecWaSeclv = customFilesManager.findObjectByUecWaSeclv(uecWaSeclv);
					if (null != uecWaPrmlv && null != uecWaSeclv) {
						if (null != uecWaPrmlv && null != uecWaSeclv) {
							bean.getPkWaPrmlv().setValue(uecWaPrmlv.getLevelName());
							bean.getPkWaSeclv().setValue(uecWaSeclv.getLevelName());
							bean.getPkWaCrt().setValue(uecWaPrmlv.getLevelName() + "/" + uecWaSeclv.getLevelName());
						}
					}
				}
				// 薪资项目
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaItem())) {
					uecSalaryProject = new UecSalaryProject();
					uecSalaryProject.setPkWaItem(uecPsndocWadoc.getPkWaItem());
					uecSalaryProject = uecSalaryProjectManager.findObjectByUecSalaryProject(uecSalaryProject);
					if (null != uecSalaryProject) {
						if (!StrUtil.isNullOrEmpty(uecSalaryProject.getName())) {
							bean.getPkWaItem().setValue(uecSalaryProject.getName());
						}
					}
				}
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPsndocWadocEditWindow(), true);
			}
		}
		if ("add".equals(opType)) {
			this.bean.getUecPsndocWadocEditWindow().setTitle("新增薪资变动情况信息");
			bean.getUecPsndocWadocEditDiv().setHeight("40%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPsndocWadocEditWindow().setTitle("编辑薪资变动情况信息");
			bean.getUecPsndocWadocEditDiv().setHeight("40%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
			uecPsndocWadoc = (UecPsndocWadoc) arg.get("uecPsndocWadoc");
			if (null != uecPsndocWadoc) {
				PubUtil.fillBeanFromPo(uecPsndocWadoc, bean);
				// 是否谈判工资
				if (!StrUtil.isEmpty(uecPsndocWadoc.getNegotiationWage())) {
					if ("Y".equals(uecPsndocWadoc.getNegotiationWage())) {
						bean.getNegotiationWage().setValue("是");
					} else if ("N".equals(uecPsndocWadoc.getNegotiationWage())) {
						bean.getNegotiationWage().setValue("否");
					}
				}
				// 是否来自于流程
				if (!StrUtil.isEmpty(uecPsndocWadoc.getWorkflowflag())) {
					if ("Y".equals(uecPsndocWadoc.getWorkflowflag())) {
						bean.getWorkflowflag().setValue("是");
					} else if ("N".equals(uecPsndocWadoc.getWorkflowflag())) {
						bean.getWorkflowflag().setValue("否");
					}
				}
				// 薪资标准类别
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaGrd())) {
					if ("1001A1100000000B2DPQ".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("集团薪资标准（新岗新体系）");
					} else if ("1001A1100000000A7FIO".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("集团薪资标准（旧岗新体系）");
					} else if ("1001A1100000000A83WH".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("内蒙古薪资标准（旧岗新体系）");
					} else if ("1001A1100000000TRON2".equals(uecPsndocWadoc.getPkWaGrd())) {
						bean.getPkWaGrd().setValue("薪资表（旧体系）");
					}
				}
				// 薪资标准
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaSeclv())
						&& !StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaPrmlv())) {
					// 薪资级别==岗位等级
					uecWaPrmlv = new UecWaPrmlv();
					uecWaPrmlv.setPkWaPrmlv(uecPsndocWadoc.getPkWaPrmlv());
					uecWaPrmlv = customFilesManager.findObjectByUecWaPrmlv(uecWaPrmlv);
					// 薪资档别
					uecWaSeclv = new UecWaSeclv();
					uecWaSeclv.setPkWaSeclv(uecPsndocWadoc.getPkWaSeclv());
					uecWaSeclv = customFilesManager.findObjectByUecWaSeclv(uecWaSeclv);
					if (null != uecWaPrmlv && null != uecWaSeclv) {
						bean.getPkWaPrmlv().setValue(uecWaPrmlv.getLevelName());
						bean.getPkWaSeclv().setValue(uecWaSeclv.getLevelName());
						bean.getPkWaCrt().setValue(uecWaPrmlv.getLevelName() + "/" + uecWaSeclv.getLevelName());
					}
				}
				// 薪资项目
				if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkWaItem())) {
					uecSalaryProject = new UecSalaryProject();
					uecSalaryProject.setPkWaItem(uecPsndocWadoc.getPkWaItem());
					uecSalaryProject = uecSalaryProjectManager.findObjectByUecSalaryProject(uecSalaryProject);
					if (null != uecSalaryProject) {
						if (!StrUtil.isNullOrEmpty(uecSalaryProject.getName())) {
							bean.getPkWaItem().setValue(uecSalaryProject.getName());
						}
					}
				}
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("edit".equals(opType)) {
			// 相关日期
			dateTransToString();
			// 变动原因
			uecPsndocWadoc.setPkChangecause((String) bean.getPkChangecause().getSelectedItem().getValue());
			// 备注
			uecPsndocWadoc.setVnote(bean.getVnote().getValue());
			// 依据文件
			uecPsndocWadoc.setVbasefile(bean.getVbasefile().getValue());
			// 修改人
			uecPsndocWadoc.setModifier(currentName);
			// 修改时间
			uecPsndocWadoc.setModifiedtime(DateUtil.getCurrenDate());

			// 发起流程
			if (ApprovalUtil.getActivityFlag()) {
				// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
				boolean isOnApproval = ApprovalUtil.isOnApproval(ApprovalConstants.STAFF_ADJUST_POST_PROC_DEF_ID,
						uecStaffInfo.getStaffId());
				if (isOnApproval) {
					ZkUtil.showError("在审状态不允许发起！", "错误提示");
					return;
				}
				/**
				 * 数据入库 修改 薪资变动情况表 uec_psndoc_wadoc 一条记录
				 */
				Long staffIdHis = uecStaffInfo.getStaffId();
				Long staffId = staffIdHis;

				Long wadocIdHis = uecPsndocWadoc.getWadocId();

				uecPsndocWadoc.setStaffId(staffId);
				uecPsndocWadoc.addStatusCdIS3();
				Long wadocIdModify = uecPsndocWadoc.getWadocId();

				String formTheme = "盟市合同制人员薪资变动修改申请";
				String startFlowJson = "{" + "formTheme: '" + formTheme + "'," + " formContent: '申请人员:"
						+ uecStaffInfo.getName() + "'," + " procDefId: '"
						+ ApprovalConstants.STAFF_ADJUST_POST_PROC_DEF_ID + "'," + " urgent: '1',"
						+ " atiActCategoryId: '70'," + " procDefKey: '" + ApprovalUtil.STAFF_ADJUST_POST + "',"
						+ " formSender: '" + currentName + "'," + " primaryId: '{" + "uec_staff_info_his:" + staffIdHis
						+ ",uec_staff_info:" + staffId + ",uec_psndoc_wadoc_his:" + wadocIdHis + ",uec_psndoc_wadoc_modi:"
						+ wadocIdModify + "'}";
				String procInstId = null;
				try {
					procInstId = ApprovalUtil.startApproval(startFlowJson);
				} catch (Exception e) {
					// 流程发起失败 插入的数据 变为失效状态
					uecPsndocWadoc.removeOnly();
					onCancel();
					ZkUtil.showError("流程发起失败！", "错误提示");
					e.printStackTrace();
				}
				if (StrUtil.isNullOrEmpty(procInstId)) {
					uecPsndocWadoc.removeOnly();
					onCancel();
					ZkUtil.showError("流程发起失败！", "错误提示");
					return;
				}
				// 第一审批人如果是发起人直接到人力审批环节
				ApprovalUtil.isCurrentAssignNames(procInstId);

				ApprovalUtil.showNextAssignee(procInstId, formTheme);
				// 关闭窗口
				this.onCancel();
			} else {
				// 更新操作
				uecPsndocWadoc.updateOnly();
				Events.postEvent("onOK", this.self, uecPsndocWadoc);
				this.onCancel();
				Messagebox.show("更新成功！");
			}
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPsndocWadocEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (bean.getVnote().getValue().length() > 100) {
			return "备注项所填内容字符数过长";
		} else if (bean.getVbasefile().getValue().length() > 100) {
			return "依据文件项所填内容字符数过长";
		}
		return null;
	}

	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		// 薪资起始日期
		uecPsndocWadoc.setBegindate(DateUtil.dateToStr(bean.getBegindate().getValue()));
		// 薪资截止日期
		uecPsndocWadoc.setEnddate(DateUtil.dateToStr(bean.getEnddate().getValue()));
		// 薪资调整让日期
		uecPsndocWadoc.setChangedate(DateUtil.dateToStr(bean.getChangedate().getValue()));
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 薪资起始日期
		bean.getBegindate().setValue(DateUtil.patternStrToDate(uecPsndocWadoc.getBegindate()));
		// 薪资截止日期
		bean.getEnddate().setValue(DateUtil.patternStrToDate(uecPsndocWadoc.getEnddate()));
		// 薪资调整让日期
		bean.getChangedate().setValue(DateUtil.patternStrToDate(uecPsndocWadoc.getChangedate()));
	}
}
